package com.xxx.ware.ware_manage_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.ware.ware_manage_sys.bean.dto.AdminSaveUpdateDto;
import com.xxx.ware.ware_manage_sys.bean.dto.AdminQueryDto;
import com.xxx.ware.ware_manage_sys.bean.dto.WebResultDto;
import com.xxx.ware.ware_manage_sys.bean.entity.admin.WmsAdmin;
import com.xxx.ware.ware_manage_sys.bean.enums.RoleEnum;
import com.xxx.ware.ware_manage_sys.bean.enums.SexEnum;
import com.xxx.ware.ware_manage_sys.bean.vo.AdminVo;
import com.xxx.ware.ware_manage_sys.mapper.WmsAdminMapper;
import com.xxx.ware.ware_manage_sys.service.IAdminService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<WmsAdminMapper, WmsAdmin> implements IAdminService {
    @Override
    public WebResultDto<AdminVo> getAdminList(AdminQueryDto adminQueryDto) {
        LambdaQueryWrapper<WmsAdmin> queryWrapper = Wrappers.lambdaQuery();
        Integer pageNo = adminQueryDto.getPageNo();
        Integer pageSize = adminQueryDto.getPageSize();
        String adminAccount = adminQueryDto.getAdminAccount();
        String adminName = adminQueryDto.getAdminName();
        String adminPhone = adminQueryDto.getAdminPhone();
        queryWrapper.eq(StringUtils.isNotBlank(adminAccount), WmsAdmin::getAdminAccount, adminAccount);
        queryWrapper.eq(StringUtils.isNotBlank(adminName), WmsAdmin::getAdminName, adminName);
        queryWrapper.eq(StringUtils.isNotBlank(adminPhone), WmsAdmin::getAdminPhone, adminPhone);
        queryWrapper.eq(WmsAdmin::getStatus, 1);   //列表只展示生效的管理员
        queryWrapper.orderByDesc(WmsAdmin::getCreateTime);
        IPage<WmsAdmin> page = new Page<>(pageNo, pageSize);
        IPage<WmsAdmin> resultPage = this.getBaseMapper().selectPage(page, queryWrapper);
        if (resultPage == null || resultPage.getRecords() == null || resultPage.getRecords().isEmpty()) {
            return WebResultDto.getDataList(pageNo, pageSize, 0L, null);
        }
        List<AdminVo> resultList = resultPage.getRecords().stream().map(
                e -> {
                    AdminVo adminVo = new AdminVo();
                    BeanUtils.copyProperties(e, adminVo);
                    adminVo.setAdminSex(SexEnum.getSex(String.valueOf(e.getAdminSex())));
                    adminVo.setAdminRole(RoleEnum.getAdminRole(String.valueOf(e.getAdminRole())));
                    adminVo.setStatus(e.getStatus().equals(1) ? "生效" : "失效");
                    return adminVo;
                }
        ).collect(Collectors.toList());
        return WebResultDto.getDataList(pageNo, pageSize, resultPage.getTotal(), resultList);
    }

    @Override
    public Boolean saveAdmin(AdminSaveUpdateDto adminSaveUpdateDto) throws Exception {
        WmsAdmin wmsAdminSole = getAdminBySoleKey(adminSaveUpdateDto.getAdminAccount(),
                adminSaveUpdateDto.getAdminName(), adminSaveUpdateDto.getAdminPhone());
        if (wmsAdminSole != null) {//如果存在报错
            throw new Exception("此管理员已存在，对应管理员账号：" + adminSaveUpdateDto.getAdminAccount() +
                                               "，对应管理员姓名：" + wmsAdminSole.getAdminName() +
                                               "，对应管理员联系方式：" + wmsAdminSole.getAdminPhone());
        } else {//不存在就新增
            WmsAdmin wmsAdmin = new WmsAdmin();
            String adminSex = adminSaveUpdateDto.getAdminSex();
            String adminRole = adminSaveUpdateDto.getAdminRole();
            BeanUtils.copyProperties(adminSaveUpdateDto, wmsAdmin);
            wmsAdmin.setAdminSex(Integer.parseInt(StringUtils.isBlank(adminSex) ? "0" : adminSex));
            wmsAdmin.setAdminRole(Integer.parseInt(StringUtils.isBlank(adminRole) ? "1" : adminSex));
            return save(wmsAdmin);
        }
    }

    @Override
    public WmsAdmin getAdminBySoleKey(String adminAccount, String adminName, String adminPhone){
        LambdaQueryWrapper<WmsAdmin> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WmsAdmin::getAdminAccount, adminAccount);
        queryWrapper.eq(WmsAdmin::getAdminName, adminName);
        queryWrapper.eq(WmsAdmin::getAdminPhone, adminPhone);
        queryWrapper.eq(WmsAdmin::getStatus, 1);
        return this.getBaseMapper().selectOne(queryWrapper);
    }

    @Override
    public Boolean updateAdmin(AdminSaveUpdateDto adminSaveUpdateDto) throws Exception {
        //修改后的数据不能和已存在的数据重复
        WmsAdmin wmsAdminSole = getAdminBySoleKey(adminSaveUpdateDto.getAdminAccount(),
                adminSaveUpdateDto.getAdminName(), adminSaveUpdateDto.getAdminPhone());
        //判断条件：账号+姓名+联系方式查询存在，同时id不相同，说明即将修改的数据和数据库的数据重复
        if (wmsAdminSole != null && !wmsAdminSole.getId().equals(adminSaveUpdateDto.getId())) {//如果存在报错
            throw new Exception("此管理员已存在，对应管理员账号：" + adminSaveUpdateDto.getAdminAccount() +
                    "，对应管理员姓名：" + wmsAdminSole.getAdminName() +
                    "，对应管理员联系方式：" + wmsAdminSole.getAdminPhone());
        }
        LambdaUpdateWrapper<WmsAdmin> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(WmsAdmin::getId, adminSaveUpdateDto.getId());
        updateWrapper.set(WmsAdmin::getAdminAccount, adminSaveUpdateDto.getAdminAccount());
        updateWrapper.set(WmsAdmin::getAdminName, adminSaveUpdateDto.getAdminName());
        updateWrapper.set(WmsAdmin::getAdminAge, adminSaveUpdateDto.getAdminAge());
        updateWrapper.set(WmsAdmin::getAdminSex, adminSaveUpdateDto.getAdminSex());
        updateWrapper.set(WmsAdmin::getAdminRole, adminSaveUpdateDto.getAdminRole());
        updateWrapper.set(WmsAdmin::getAdminPhone, adminSaveUpdateDto.getAdminPhone());
        return update(updateWrapper);
    }

    @Override
    public Boolean removeAdmin(Long id) {
        WmsAdmin wmsAdmin = new WmsAdmin();
        wmsAdmin.setId(id);
        wmsAdmin.setStatus(2);
        return updateById(wmsAdmin);
    }
}

